package com.boarsoft.boar.batch.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.boarsoft.boar.batch.biz.BatchExecutionBiz;
import com.boarsoft.boar.batch.entity.BatchExecution;
import com.boarsoft.boar.batch.service.BatchExecutionService;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.RandomUtil;

public class BatchExecutionServiceImpl implements BatchExecutionService {

    private final static Logger log = LoggerFactory.getLogger(BatchExecutionServiceImpl.class);

    @Autowired
    protected BatchExecutionBiz executionBiz;

    @Override
    public BatchExecution get(String id) {
        return executionBiz.get(id);
    }

    @Override
    public BatchExecution getByBatchId(String batchId) {
        return executionBiz.getBybatchId(batchId);
    }

    @Override
    public List<BatchExecution> getAllByBatchId(String batchId) {
        return executionBiz.getAllByBatchId(batchId);
    }

    @Override
    public List<BatchExecution> getSonNods(String id) {
        return executionBiz.getSonNods(id);
    }

    @Override
    public List<BatchExecution> list(String[] ids) {
        return executionBiz.list(ids);
    }

    @Override
    public PagedResult<BatchExecution> list(String infoId, String startTime, String endTime, int pageNo, int pageSize) {
        return executionBiz.list(infoId, startTime, endTime, pageNo, pageSize);
    }

    @Override
    public PagedResult<BatchExecution> list(String catalog, String key, String startTime, String endTime, int pageNo, int pageSize) {
        return executionBiz.list(catalog, key, startTime, endTime, pageNo, pageSize);
    }

    @Override
    public String save(BatchExecution o) {
        log.info("start save BatchExecution id:{},batchId:{},parentId:{}", o.getId(), o.getBatchId(), o.getParentId());
        if (Util.strIsEmpty(o.getBatchId())) {
            return "BatchId 不允许为空";
        }

        String id = RandomUtil.randomUUID();
        o.setId(id);

        //父id不为空时需要重置path字段的值
        String path = null;
        if (Util.strIsNotEmpty(o.getParentId())) {
            BatchExecution parent = executionBiz.get(o.getParentId());
            if (parent.getPath() != null) {
                path = parent.getPath() + "/" + id;
            } else {
                path = id;
            }
        } else {
            path = id;
        }
        o.setPath(path);
        o.setLevel(path.contains("/") ? (short) path.split("/").length : (short) 1);
        log.info("end save BatchExecution id:{},batchId:{},parentId:{}", o.getId(), o.getBatchId(), o.getParentId());
        return executionBiz.save(o);
    }

    @Override
    public String update(BatchExecution o) {
        log.info("start update BatchExecution id:{},batchId:{},parentId:{}", o.getId(), o.getBatchId(), o.getParentId());
        //只允许修改执行状态及异常监息
        BatchExecution be = executionBiz.get(o.getId());
        if (be == null) {
            return "没有找到对应的记录监息";
        }
        be.setStatus(o.getStatus());
        be.setError(o.getError());
        return executionBiz.update(be);
    }

    @Override
    public BatchExecution last(String defId) {
        return executionBiz.last(defId);
    }

    @Override
    public List<BatchExecution> list(short status) {
        return executionBiz.list(status);
    }

    @Override
    public List<BatchExecution> list() {
        return executionBiz.list();
    }

    @Override
    public List<BatchExecution> list(String id) {
        return executionBiz.list(id);
    }

    @Override
    public List<BatchExecution> list(String startTime, String endTime) {
        return executionBiz.list(startTime, endTime);
    }

    @Override
    public PagedResult<BatchExecution> list(String startTime, String endTime, int pageNo, int pageSize) {
        return executionBiz.list(startTime, endTime, pageNo, pageSize);
    }

    @Override
    public PagedResult<Object[]> listExecuting(int pageNo, int pageSize) {
        return executionBiz.listExecuting(pageNo, pageSize);
    }

    @Override
    public void delete(String id) {
        log.info("start delete BatchExecution id:{}", id);
        executionBiz.delete(id);
    }

    @Override
    public int updateStatusSuccess(String id) {
        return executionBiz.updateStatusSuccess(id);
    }

    @Override
    public int updateStatusFailed(String id) {
        return executionBiz.updateStatusFailed(id);
    }
}
